/**
 * 訂單查询
 */
function waithtml(num,address,createdDt,orderid){
	return '<div class="wait">'
	+'<div class="prompt">'
	+'<span>待确认</span>'
	+'</div>'
	+'<div class="top" onclick=onclickall('+orderid+')>'
	+'<p>预约收油数量</p>'
	+'<h2>'+num+'</h2>'
	+'<span>桶</span>'
	+'</div>'
	+'<div class="bottom" onclick=onclickall('+orderid+')>'
	+'<div class="address">'
	+'<i class="icon iconfont icon-map"></i>'
	+'<span>'+address+'</span>'
	+'</div>'
	+'<div class="time">'
	+'	<i class="icon iconfont icon-time"></i>'
	+'<p>'
	+'<span>发布时间</span>'
	+'<span>'+createdDt+'</span>'
	+'</p>'
	+'</div>'
	+'</div>'
	+'<a class="sure" href="javascript:void(0)" onclick=updateorders('+orderid+',3)>确认任务</a>'
	+'</div>'
}
function nowtml(num,address,createdDt,orderid){
	return '<div class="now">'
	+'<div class="prompt">'
	+'<span>进行中</span>'
	+'</div>'
	+'<div class="top" onclick=onclickall('+orderid+')>'
	+'<p>预约收油数量</p>'
	+'<h2>'+num+'</h2>'
	+'<span>桶</span>'
	+'</div>'
	+'<div class="bottom" onclick=onclickall('+orderid+')>'
	+'<div class="address">'
	+'<i class="icon iconfont icon-map"></i>'
	+'<span>'+address+'</span>'
	+'</div>'
	+'<div class="time">'
	+'<i class="icon iconfont icon-time"></i>'
	+'<p>'
	+'<span>发布时间</span>'
	+'<span>'+createdDt+'</span>'
	+'</p>'
	+'</div>'
	+'</div>'
	+'<a class="look" onclick=onclickall('+orderid+')>查看任务</a>'
	+'</div>'
}
function finishhtml(num,address,createdDt,orderid){
	return '<div class="finish">'
	+'<div class="prompt">'
	+'<span>已结束</span>'
	+'</div>'
	+'<div class="top" onclick=onclickall('+orderid+')>'
	+'<p>实际收油数量</p>'
	+'<h2>'+num+'</h2>'
	+'<span>桶</span>'
	+'</div>'
	+'<div class="bottom" onclick=onclickall('+orderid+')>'
	+'<div class="address">'
	+'<i class="icon iconfont icon-map"></i>'
	+'<span>'+address+'</span>'
	+'</div>'
	+'<div class="time">'
	+'<i class="icon iconfont icon-time"></i>'
	+'<p>'
	+'<span>发布时间</span>'
	+'<span>'+createdDt+'</span>'
	+'</p>'
	+'</div>'
	+'</div>'
	+'<a class="look" onclick=onclickall('+orderid+')>查看任务</a>'
	+'</div>'
}
function findlist(phone,orderid,roleId) {
	var jsonstr = "filterscount=0&groupscount=0&pagenum=0&pagesize=1000&recordstartindex=0&recordendindex=1000&roleId="+roleId+"&phone="+phone+"&iphone="+phone+"&orderid="+orderid+"";
	$.ajax({
		cache: false,
		type: "post",
		datatype: 'json',
		contentType: 'application/json;charset=UTF-8',
		data: jsonstr,
		url: ''+httpipstr+'alfa-mobile-ws/rest/order/findlist?mobiletoken=' + getCookie("mobiletokensalesman") + '',
		success: function(data) {
			if(orderid==""){
				if(data.rows==""){
					$(".main").html('<i class="icon iconfont icon-weibiaoti1"></i><p>暂时没有消息哟。。。</p>');
				}else{
					var headerjudge=false;
					$(".main").html("");
					var str='<div class="prompt">'
						+'<img src="../img/warning@2x.png" /> <span>您好，你当前有待确认任务，请你尽快确认...</span>'
						+'</div>'
					$.each(data.rows, function (n, value) {
						if(value.orgstatus=="2"){
							headerjudge=true;
							$(".main").append(waithtml(value.num,value.address,value.createdDt,value.orderid));
						}else if(value.orgstatus=="3"){
							$(".main").append(nowtml(value.num,value.address,value.createdDt,value.orderid));
						}else if(value.orgstatus=="4"){
							$(".main").append(finishhtml((!value.realnum ||value.realnum=="undefined"? "0" : ""+value.realnum+""),value.address,value.createdDt,value.orderid));
						}
					})
						$(".header").children(".prompt").remove();
					if(headerjudge){
						$(".header").append(str) 
					}
				}
			}else{
				
				$("#name").html(data.rows[0].orgname);
				$("#companyname").html(data.rows[0].orgname);
				$("#address").html(data.rows[0].address);
				$("#num").html(data.rows[0].num);
				$("#createdDt").html(data.rows[0].createdDt);
				$("#iphone").html(data.rows[0].iphone);
				if(data.rows[0].orgstatus=="2"){
					$(".receipts").remove();
					$(".number").remove();
					$(".wait-time").html(data.rows[0].createdDt);
				}else if(data.rows[0].orgstatus=="3"){
					$(".receipts").remove();
					$(".number").remove();
					 $("#realnum").val(data.rows[0].num)
					$(".wait-time").html(data.rows[0].createdDt);
					$(".now-time").html(data.rows[0].confirmDt);
					$(".wait").addClass("successcolor");
					$(".now").addClass("failurecolor");
					$(".sure").attr("name","4");
										
				}else if(data.rows[0].orgstatus=="4"){
					$(".number").children("h2").html((!data.rows[0].realnum  ||data.rows[0].realnum =="undefined"? "0" : ""+data.rows[0].realnum +""));
					$(".wait").addClass("successcolor");
					$(".now").addClass("successcolor");
					$(".finish").addClass("successcolor");
					$(".wait-time").html(data.rows[0].createdDt);
					$(".now-time").html(data.rows[0].confirmDt);
					$(".finish-time").html(data.rows[0].updatedDt);
					$(".bottom").remove();
					
				}
				
				
			}
			layer.closeAll();
		},
        error: function (XMLHttpRequest, textStatus, errorThrown) {
        	location.href="../salesman/login.jsp";
        }
	});
}
/**
 * 订单确认
 * @returns
 */
function updateorders(orderid,orgstatus){
	var json = "";
	var url="";
	layui.use('layer', function(){
		layer.load(2, {time: 3*1000})
	})
	if(orgstatus=="3"){
		 json = {"orderid": ""+orderid+"","orgstatus":""+orgstatus+""};
		 url=""+httpipstr+"alfa-mobile-ws/rest/order/updateorders";
		 updateorderhttp(url,json)
	}else{
			 url=""+httpipstr+"alfa-mobile-ws/rest/order/updateorders";
			 updateorderwindows('#popupwindows',url,orderid,orgstatus);
		 
	}
		
}
function updateorderhttp(url,json){
	$.ajax({
		cache: false,
		type: "post",
		datatype: 'json',
		contentType: 'application/json;charset=UTF-8',
		url: ''+url+'?mobiletoken=' + getCookie("mobiletokensalesman") + '',
		data: JSON.stringify(json),
		success: function(data) {
			if(data.message=="1"){
				/*findlist(getCookie("iphone"), "", 9)*/
				location.reload() 
			}else{
				
			}
		}
	});
}
function onclickall(orderid){
	/*findlist(getCookie("iphone"), orderid, 9)*/
	layui.use('layer', function(){
		layer.load(2, {time: 3*1000})
	})
	location.href="../salesman/task-details.jsp?orderid="+orderid
}
var orderid1="";
var orgstatus1=""
function updateorderwindows(id,url,orderid,orgstatus){
	orderid1=orderid;
	orgstatus1=orgstatus;
	layui.use('layer', function(){ //独立版的layer无需执行这一句
	layer.open({
        type: 1
        ,title:false
        ,content: $(''+id+'')
        ,area: ['250px', '200px']
        ,yes: function(){
        	/*var realnum=$("#realnum").val();
        	if(realnum==""){
        		  layer.closeAll();
        	}else{
        		var json = {"orderid": ""+orderid+"","orgstatus":""+orgstatus+"","realnum":""+realnum+""};
            	updateorderhttp(url,json);
            	 layer.closeAll();
        	}*/
        	
        } 
      })
	})
}
function newupdateorderhttp(){
	var realnum=$("#realnum").val()
	var iphonetype=true;
	if(realnum==""){
		iphonetype=false;
		 tippopup("#realnum","请输入实际收油桶数")
	}if(!realnum.match(/^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$/  )){
		iphonetype=false;
		tippopup("#num","请输入有效的桶数")
	}
	if(realnum.toString().length>5){
		iphonetype=false;
		tippopup("#num","请输入有效的桶数")
	}else if(iphonetype){
		var json = {"orderid": ""+orderid1+"","orgstatus":""+orgstatus1+"","realnum":""+realnum+""};
		$.ajax({
			cache: false,
			type: "post",
			datatype: 'json',
			contentType: 'application/json;charset=UTF-8',
			url: ''+httpipstr+'alfa-mobile-ws/rest/order/updateorders?mobiletoken=' + getCookie("mobiletokensalesman") + '',
			data: JSON.stringify(json),
			success: function(data) {
				if(data.message=="1"){
					/*findlist(getCookie("iphone"), "", 9)*/
					location.reload() 
				}else{
					
				}
			}
		});
		layer.closeAll();
	}
	
}